Frontend.ctrl.search = function() {
	var store = {

	// filter default option
	}, _filter = function(self) {
		var i = self,
			s = i.parent().find('span'),
			v = i.val();

		if(v.length > 0) s.hide();
		else s.fadeIn('fast');

	// bind for filter
	}, fields = function() {
		$('input[type="text"]', '#search').each(function() {
			var self = $(this);

			_filter(self);
			self.on('input', function(){ _filter(self); });
		});

	// on form submit intercept
	}, intercept = function() {
		// on query submit
		$('form#form_search', '#search').on('submit', function(e){
			e.preventDefault();
			var post = $(this).serialize(),
				b = $('button', '#form_search');

			// for every form field
			$('input[type="text"]', '#form_search').each(function() {
				var o = $(this);
					n = o.attr('name'),
					N = n.charAt(0).toUpperCase() + n.substr(1);

				// check if blank
				if(!$.trim(o.val()).length > 0) {
					Frontend.noty({type: 'error', text: N +' is blank'});
					o.addClass('error');
					o.val('');
					_filter(o);
				}
			});

			// check for errors
			var error = false;
			$('input[type="text"]', '#form_search').each(function() {
				if($(this).hasClass('error'))
					error = true;
			});

			// stop if errors found
			if(error)
				return false;

			// do search procedure
			b.attr('disabled', 'disabled');
			Frontend.ajax({type: 'post', data: post, url: 'search/submit', success: function(data){
				b.removeAttr('disabled');

				// go to index
				Frontend.go('search/results/' + data.string);
			}, error: function(data) {
				b.removeAttr('disabled');

				// mark error
				$('input[type="text"]', '#form_search').addClass('error');

				// noty
				Frontend.noty({type: 'error', text: data.msg});
			}});
		});

		// on button click
		$('button', 'form.search_handlers').on('click', function(e) {
			e.preventDefault();
			var b = $(this);

			// reply
			if(b.data('handle') == 'refresh')
				Frontend.go('search/results/' + Frontend.location.path.id);
		});

	// bind events
	}, bind = function() {
		// on any input change, loose the error
		$('input[type="text"]', '#search').on('input', function() {
			$(this).removeClass();
		});

		// stop clicking on inactive links
		$('ul.pagination > li > a.inactive').on('click', function(e) {
			e.preventDefault();
		});

		// on post finder
		$('a.find_post', '#search').on('click', function(e) {
			e.preventDefault();
			var ref = $(this);

			// find correct topic and page
			Frontend.ajax({type: 'post', data: ref.data(), url: 'forum/find', success: function(data) {
				Frontend.go('forum/topic/' + data.link);

				// listen for the lick event
				$('div.page', 'div.site').bind('licked', function() {
					var r = $('tr#reply_'+ ref.data('reply'), '#forum');

					// scroll to element and highlight it (sorta)
					if(!Frontend.pinpoint(r)) {
						$("html, body").animate({scrollTop: (r.offset().top - 10) - $("html, body").offset().top}, 200, 'swing').promise().done(function() {
							r.flare();
						});
					} else r.flare();

					// unbind :(
					$(this).unbind('licked');
				});
			}});
		});

	// highlight words
	}, highlight = function() {
		var w = $('div.highlight_words', '#search');
		if(!w.length > 0)
			return;

		// do the highlighting only on topic name and message
		$("a.find_post, div.msg", 'div#search').highlight($.parseJSON(w.html()));

	// constructor
	}, __construct = function() {
		fields();
		bind();
		intercept();
		highlight();
		Frontend.ctrl.forum.images();

		// select first element (not a button, not hidden) from the first form
		$('form:first *:input[type!=hidden]:not(button):first').focus();
	};

	// public, yay
	return {
		init: __construct
	};
}();